package com.xinggq.user.service.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.xinggq.user.service.dto.tenant.TenantCreateRequest;
import com.xinggq.user.service.dto.tenant.TenantVO;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;

/**
 * 租户实体类
 *
 * @author system
 * @since 2025/1/10
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_tenant")
public class SysTenantEntity {

  /**
   * 租户ID，使用分布式ID生成器
   */
  @TableId(value = "id", type = IdType.ASSIGN_ID)
  private Long id;

  /**
   * 租户名称
   */
  private String tenantName;

  /**
   * 租户编码，唯一标识
   */
  private String tenantCode;

  /**
   * 租户域名
   */
  private String domain;

  /**
   * 联系人
   */
  private String contactPerson;

  /**
   * 联系电话
   */
  private String contactPhone;

  /**
   * 联系邮箱
   */
  private String contactEmail;

  /**
   * 租户Logo URL
   */
  private String logoUrl;

  /**
   * 状态：1-正常，0-禁用，-1-删除
   */
  private Integer status;

  /**
   * 到期时间
   */
  private LocalDateTime expireTime;

  /**
   * 最大用户数
   */
  private Integer maxUsers;

  /**
   * 创建人ID
   */
  private Long createdBy;

  /**
   * 创建时间
   */
  private LocalDateTime createdAt;

  /**
   * 更新人ID
   */
  private Long updatedBy;

  /**
   * 更新时间
   */
  private LocalDateTime updatedAt;
  /**
   * 描述
   */
  private String description;

  /**
   * 设置创建信息
   */
  public void setCreateInfo(Long userId) {
    LocalDateTime now = LocalDateTime.now();
    this.createdAt = now;
    this.updatedAt = now;
    this.createdBy = userId;
    this.updatedBy = userId;
    if (this.status == null) {
      this.status = 1; // 默认正常状态
    }
  }

  /**
   * 设置更新信息
   */
  public void setUpdateInfo(Long userId) {
    this.updatedAt = LocalDateTime.now();
    this.updatedBy = userId;
  }

  /**
   * 转换为TenantVO
   */
  public TenantVO toTenantVO() {
    TenantVO vo = new TenantVO();
    vo.setId(this.getId());
    vo.setTenantCode(this.getTenantCode());
    vo.setTenantName(this.getTenantName());
    vo.setDomain(this.getDomain());
    vo.setContactPerson(this.getContactPerson());
    vo.setContactPhone(this.getContactPhone());
    vo.setContactEmail(this.getContactEmail());
    vo.setStatus(this.getStatus());
    vo.setExpireTime(this.getExpireTime());
    vo.setMaxUsers(this.getMaxUsers());
    vo.setCreateTime(this.getCreatedAt());
    vo.setUpdateTime(this.getUpdatedAt());
    vo.setCreateBy(this.getCreatedBy());
    vo.setUpdateBy(this.getUpdatedBy());
    vo.setDescription(this.getDescription());
    return vo;
  }

  public static SysTenantEntity fromTenantVO(TenantCreateRequest request) {
    SysTenantEntity tenant = new SysTenantEntity();
    tenant.setTenantCode(request.tenantCode());
    tenant.setTenantName(request.tenantName());
    tenant.setDomain(request.domain());
    tenant.setContactPerson(request.contactPerson());
    tenant.setContactPhone(request.contactPhone());
    tenant.setContactEmail(request.contactEmail());
    tenant.setExpireTime(request.expireTime());
    tenant.setMaxUsers(request.maxUsers());
    tenant.setDescription(request.description());
    return tenant;
  }
}